<?php
/*
 * @Author: 罗永生
 * @Date: 2021-11-22 20:00:55
 * @LastEditors: 罗永生
 * @LastEditTime: 2021-11-22 20:19:03
 * @Description:
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\UserModel;

class UserController extends Controller
{
    //
    public function test1()
    {
        // $Test1 = Db::table('test1');
        // $res = $Test1->find(1);
        // $res = UserModel::find(2)->toArray();


        // $res = UserModel::first(['id', 'name'])->toArray();
        // $res = UserModel::whereIn('id', [3])->get(['id', 'name'])->toArray();

        // echo $res[0]['name'];
        // die;

        // $res = UserModel::create(['name' => 'bbb']);
        // $res = $Test1->create(['name' => 'bbb']);
        // $res = UserModel::create(['name' => 'bbb']);

        // $model = new UserModel;
        // $model->name = 'ccc';
        // $res = $model->save();

        // $res = UserModel::where('id', '<', 6)->chunk(2, function ($list) {
        //     print_r($list->toArray());
        // });

        // $res = UserModel::where('id', '<', 6)->orderBy('id');

        // $res1 = $res->get()->toArray();

        // $res2 = $res->get()->each(function ($item) {
        //     $item['id'] += 1;
        // })->toArray();

        // print_r($res1);

        // foreach (UserModel::where('id', '<', 6)->cursor() as $flight) {
        //     print_r($flight->toArray());
        // }

        // $res = UserModel::findOrFail(100);

        // $res = UserModel::where(['name' => 'ccc'])->delete();
        // $res = UserModel::destroy([7,8]);
        // $res = UserModel::where('name', 'ccc')->destroy();

        // $res = UserModel::where('id', '<', 8)->orderBy('id')->get()->toArray();

        // $res = UserModel::get()->toArray();
        // $res = UserModel::find(100);
        // $res = UserModel::where('id', 100)->get();
        // $res and $res = $res->toArray();
        
        // $res = UserModel::find(1)->hobbys;
        // $res and $res = $res->toArray();

        // $res = UserModel::with('role', 'hobbys', 'roles')->get();

        // $res = UserModel::with('role')->get();

        // $res = UserModel::with('hobbys')->get();
        $res = UserModel::has('hobbys', '>=', 1)->get();


        // $res = UserModel::with(['role' => function ($query) {
        //     $query->select(['id', 'role']);
        // }])->get();


        $res and $res = $res->toArray();

        // $res = UserModel::with(['hobbys' => function ($query) {
        //     // $query->select(['id', 'user_id', 'hobby']);
        //     $query->value('hobby');
        // }])->with('roles')->get();
        // $res and $res = $res->toArray();


        print_r($res);
    }
}
